home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / ColorSync SDK / Sample Code / CSDemo 2.1 / ShellSources / aeUtils.c next >
Encoding:
Text File  |  1997-06-13  |  4.4 KB  |  159 lines  |  [TEXT/CWIE]

  1. // Simple framework for Macintosh sample code
  2. //
  3. // David Hayward and Nick Thompson 
  4. // Developer Technical Support
  5. // AppleLink: DEVSUPPORT
  6. //
  7. // Copyrite 1995, Apple Computer,Inc
  8. //
  9. // This file contains the AppleEvent utility routines.
  10. // 
  11. // 9/16/94    nick    first cut
  12. // 11/16/94    david    added a global gAE_available to aeGlobals.c
  13. //                    changeg AE_available to set/clear the global
  14.  
  15.  
  16. #include <Gestalt.h>
  17. #include <AppleEvents.h>
  18.  
  19. #include "aeUtils.h"
  20.  
  21.  
  22.  
  23. /**\
  24. |**| ==============================================================================
  25. |**| PUBLIC GLOBALS
  26. |**| ==============================================================================
  27. \**/
  28. AEAddressDesc        gSelfPSNAddress;        // A self-addressed address descriptor record using GetCurrentProcess()
  29. AEAddressDesc        gSelfAddress;            // A self-addressed address descriptor record using kCurrentProcess
  30. ProcessSerialNumber    gSelfPSN;                // This application's psn
  31. AEDesc                gNullDesc;                // A null descriptor record
  32.  
  33.  
  34. /**\
  35. |**| ==============================================================================
  36. |**| PRIVATE GLOBALS
  37. |**| ==============================================================================
  38. \**/
  39. OSErr        gAE_present_err = 1;
  40. OSErr        gAE_available_err = 1;
  41. OSErr        gAE_initialized_err = 1;
  42.  
  43.  
  44. /**\
  45. |**| ==============================================================================
  46. |**| PUBLIC FUNCTIONS
  47. |**| ==============================================================================
  48. \**/
  49.  
  50.  
  51. /*------------------------------------------------------------------------------*\
  52.     AE_present
  53.  *------------------------------------------------------------------------------*
  54.         This function returns noErr if apple event support is present 
  55.         (i.e passes gestalt) and returns an error otherwise.
  56.         It also sets the private global gAE_present_err based on the result.
  57. \*------------------------------------------------------------------------------*/
  58. OSErr AE_present ( void )
  59. {
  60.     OSErr err;
  61.     long response;
  62.     
  63.     if ( !gAE_present_err )
  64.         return noErr;
  65.  
  66.     err = Gestalt(gestaltAppleEventsAttr,&response) ;
  67.     if (!err)
  68.         if ((response & (1<<gestaltAppleEventsPresent)) == 0 )
  69.             err = 1 ;                     // should return something else
  70.     
  71.     gAE_present_err = err;
  72.     return err;
  73. }
  74.  
  75.  
  76. /*------------------------------------------------------------------------------*\
  77.     AE_available
  78.  *------------------------------------------------------------------------------*
  79.         This function returns noErr if apple event support is available
  80.         (i.e present and initialized) to the application and returns an error otherwise.
  81.         It also sets the global gAE_available_err based on the result.
  82. \*------------------------------------------------------------------------------*/
  83. OSErr    AE_available ( void ) 
  84. {
  85.     OSErr err;
  86.  
  87.     if ( !gAE_available_err )
  88.         return noErr;
  89.     
  90.     err = AE_present();
  91.     if ( !err )
  92.         err = AE_initialize();
  93.     
  94.     gAE_available_err = err;
  95.      return err ;
  96. }
  97.  
  98. /*------------------------------------------------------------------------------*\
  99.     AE_initialize
  100.  *------------------------------------------------------------------------------*
  101.         This function initializes all the apple event stuff.
  102.         It should be called early in the application.
  103. \*------------------------------------------------------------------------------*/
  104. OSErr AE_initialize ( void ) 
  105. {
  106.     OSErr err;
  107.     ProcessSerialNumber    currPSN;
  108.  
  109.     if ( !gAE_initialized_err )
  110.         return noErr;
  111.  
  112.     err = AE_present();
  113.     if ( !err )
  114.     {
  115.         currPSN.highLongOfPSN = 0;
  116.         currPSN.lowLongOfPSN = kCurrentProcess;    // Use this instead of GetCurrentProcess *//
  117.  
  118.         err = GetCurrentProcess(&gSelfPSN);
  119.  
  120.         err = AECreateDesc(    typeProcessSerialNumber,
  121.                             (Ptr)&gSelfPSN,
  122.                             sizeof(ProcessSerialNumber),
  123.                             &gSelfPSNAddress) ;
  124.  
  125.         err = AECreateDesc(    typeProcessSerialNumber,
  126.                             (Ptr)&currPSN,
  127.                             sizeof(ProcessSerialNumber),
  128.                             &gSelfAddress) ;
  129.  
  130.         gNullDesc.descriptorType = typeNull;        // Initialize the global null descriptor record.
  131.         gNullDesc.dataHandle = nil;
  132.     }
  133.         
  134.     gAE_initialized_err = err;
  135.     return err;
  136. }
  137.  
  138.  
  139. //-----------------------------------------------------------------------
  140. // utility routine to ensure we got all required parameters from the 
  141. // appleevent passed in.
  142.  
  143. OSErr MyGotRequiredParams ( AppleEvent *theAppleEvent )
  144. {
  145.     DescType returnedType;
  146.     Size actualSize;
  147.     OSErr err ;
  148.     
  149.     err = AEGetAttributePtr(theAppleEvent,keyMissedKeywordAttr,typeWildCard,
  150.                             &returnedType,nil,0,&actualSize) ;
  151.     if (err == errAEDescNotFound)
  152.         err = noErr ;
  153.     else if (err==noErr)
  154.         err = errAEEventNotHandled;
  155.     return err ;
  156. }
  157.     
  158.  
  159.